<template>
  <div>
    <el-form
      ref="formone"
      :model="form"
      label-position="left"
      label-width="140px"
      size="mini"
      :rules="rules"
    >
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item label="编组方式：" prop="groupMethod">
            <el-select v-model="form.groupMethod" placeholder="请选择">
              <el-option
                v-for="item in dict.type.sys_group_method"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="配置类型：" prop="configurationType">
            <el-select
              v-model="form.configurationType"
              placeholder="请选择"
              disabled
            >
              <el-option
                v-for="item in dict.type.sys_configuration_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="所属平台：" prop="nodeId">
            <treeselect
              v-model="form.nodeId"
              :options="menuOptions"
              :normalizer="normalizer"
              :show-count="true"
              placeholder="请选择所属节点"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="16">
          <div class="addBox">
            <!-- <input id='file-upload' type="file" @change="handleFileUpload"> -->
            <el-button type="success" @click="modeDown">模板下载</el-button>
            <el-button type="danger" @click="openForm">上传标准表单</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <button class="add-btn" @click="addList">+</button>
    <div class="add-table" :loading="loading">
      <div class="addd-table-column add-table-title">
        <div style="width: 350px">系统名称</div>
        <div style="width: 350px">子系统名称</div>
        <div style="width: 320px">子系统构型码</div>
        <div style="width: 200px">数量</div>
        <div style="width: 170px">单价</div>
        <div style="width: 90px">操作</div>
      </div>
      <div
        class="addd-table-column add-table-list"
        v-for="(_item, index) in form.quote"
        :key="index"
      >
        <!-- 系统名称 -->
        <div style="width: 350px">
          <el-select v-model="_item.systemName" placeholder="请选择">
            <el-option
              v-for="item in dict.type.sys_system_name"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>
        <!-- 子系统名称 -->
        <div style="width: 350px">
          <el-select v-model="_item.subSystem" placeholder="请选择">
            <el-option
              v-for="item in dict.type.sys_sub_system_name"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>
        <!-- 子系统构型码 -->
        <div style="width: 320px">
          <el-select
            v-model="_item.subSystemConfigurationCode"
            placeholder="请选择"
          >
            <el-option
              v-for="item in dict.type.sub_system_configuration_code"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>
        <div style="width: 200px">
          <el-input-number
            v-model="_item.nums"
            :min="0"
            label="數量"
            @change="handlePrice('basics')"
            :precision="1"
          ></el-input-number>
        </div>
        <div style="width: 170px">
          <el-input
            v-model="_item.domesticGoodsPrice"
            placeholder="请输入单价"
            disabled
            @input="handlePrice('basics')"
          ></el-input>
        </div>
        <div style="width: 90px">
          <el-button
            size="mini"
            type="text"
            class="ordinary-btn"
            @click="handleDelete(index)"
            ><svg-icon
              icon-class="table-column-delete"
              style="width: 16px; height: 16px"
            />
            删除
          </el-button>
        </div>
      </div>
    </div>
    <div class="mt20">
      <span>基础配置总价：</span>
      <span style="display: inline-block; width: 300px"
        ><el-input
          v-model="form.totalPrice"
          @input="handlePrice"
          placeholder="请输入总价"
        ></el-input
      ></span>
    </div>
    <div class="mt20 newAddBtn">
      <button class="add-btn" @click="newAddList">+</button>
    </div>
    <el-form
      ref="form"
      :model="form"
      label-position="left"
      label-width="100px"
      size="mini"
    >
      <el-row :gutter="20">
        <el-col :span="8">
          <div style="height: 10px"></div>
        </el-col>
        <el-col :span="8">
          <el-form-item label="配置类型：" prop="configurationType">
            <el-select
              v-model="newForm.configurationType"
              placeholder="请选择"
              disabled
            >
              <el-option
                v-for="item in dict.type.sys_configuration_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="add-table">
      <div class="addd-table-column add-table-title">
        <div style="width: 350px">系统名称</div>
        <div style="width: 350px">子系统名称</div>
        <div style="width: 320px">子系统构型码</div>
        <div style="width: 200px">数量</div>
        <div style="width: 170px">单价</div>
        <div style="width: 90px">操作</div>
      </div>
      <div
        class="addd-table-column add-table-list"
        v-for="(_item, index) in newForm.quote"
        :key="index"
      >
        <!-- 系统名称 -->
        <div style="width: 350px">
          <el-select v-model="_item.systemName" placeholder="请选择">
            <el-option
              v-for="item in dict.type.sys_system_name"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>
        <!-- 子系统名称 -->
        <div style="width: 350px">
          <el-select v-model="_item.subSystem" placeholder="请选择">
            <el-option
              v-for="item in dict.type.sys_sub_system_name"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>
        <!-- 子系统构型码 -->
        <div style="width: 320px">
          <el-select
            v-model="_item.subSystemConfigurationCode"
            placeholder="请选择"
          >
            <el-option
              v-for="item in dict.type.sub_system_configuration_code"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>
        <div style="width: 200px">
          <el-input-number
            v-model="_item.nums"
            :min="0"
            label="數量"
            @change="handlePrice"
            :precision="1"
          ></el-input-number>
        </div>
        <div style="width: 170px">
          <el-input
            v-model="_item.domesticGoodsPrice"
            placeholder="请输入单价"
            @input="handlePrice"
          ></el-input>
        </div>
        <div style="width: 90px">
          <el-button
            size="mini"
            type="text"
            class="ordinary-btn"
            @click="newHandleDelete(index)"
            ><svg-icon
              icon-class="table-column-delete"
              style="width: 16px; height: 16px"
            />
            删除
          </el-button>
        </div>
      </div>
    </div>
    <div class="mt20">
      <span>总价：</span>
      <span style="display: inline-block; width: 300px"
        ><el-input v-model="total" placeholder="请输入总价"></el-input
      ></span>
    </div>

    <el-dialog
      title="上传文件"
      :visible.sync="importOpen"
      width="400px"
      append-to-body
      :destroy-on-close="true"
    >
      <div class="fileParBox" v-loading="loading">
        <div>上传文件</div>
        <input id="file-upload" type="file" @change="handleFileUpload" />
        <!-- <template-window :form-data="formData" :template-type="templateType" /> -->
      </div>
      <div v-if="file && file.name">{{ file.name }}</div>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          size="mini"
          class="tableAdd"
          :disabled="!file"
          @click="upForm"
        >
          确定
        </el-button>
        <el-button
          class="dialogCancel"
          type="primary"
          size="mini"
          @click="
            importOpen = false;
            file = null;
          "
          >关闭</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { importQuoteData } from "@/api/importQuoteData/index";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import axios from 'axios'
import Cookie from "js-cookie";
import { details } from "@/api/offerManage/index";
export default {
  name: "add",
  dicts: [
    "sys_system_name",
    "sys_sub_system_name",
    "sub_system_configuration_code",
    "sys_group_method",
    "sys_configuration_type",
  ],
  components: { Treeselect },
  props: {
    menuOptions: {
      type: Array,
      default: null,
    },
    normalizer: {
      type: Function,
    },
    title: {
      type: String
    },
  },
  data() {
    return {
      total: 0,
      form: {
        groupMethod: "",
        configurationType: "1",
        nodeId: undefined,
        totalPrice: "",
        quoteGroupId: '',
        quote: [
          {
            systemName: "",
            subSystem: "",
            subSystemConfigurationCode: "",
            nums: 0,
            domesticGoodsPrice: "",
          },
        ],
      },
      newForm: {
        configurationType: "2",
        totalPrice: "",
        quoteGroupId: '',
        quote: [
          {
            systemName: "",
            subSystem: "",
            subSystemConfigurationCode: "",
            nums: 0,
            domesticGoodsPrice: "",
          },
        ],
      },
      file: '',
      loading: false,
      importOpen: false,
      getInfo: {},
      rules: {
        groupMethod: [
          {
            required: true,
            message: "请选择编组方式",
            trigger: "change",
          },
        ],
        nodeId: [
          {
            required: true,
            message: "请选择所属平台",
            trigger: "change",
          },
        ]
      },
    };
  },
  methods: {
    // 基础配置添加列表
    addList() {
      this.form.quote.push({
        systemName: "",
        subSystem: "",
        subSystemConfigurationCode: "",
        nums: 0,
        domesticGoodsPrice: "",
      });
    },
    // 可选配置添加列表
    newAddList() {
      this.newForm.quote.push({
        systemName: "",
        subSystem: "",
        subSystemConfigurationCode: "",
        nums: 0,
        domesticGoodsPrice: "",
      });
    },
    // 计算总价
    handlePrice(type) {
      // let total = 0;
      let newTotal = 0;
      // if (type === "basics") {
      //   this.form.quote.forEach((item) => {
      //     total += item.nums * Number(item.domesticGoodsPrice);
      //   });
      //   this.form.totalPrice = total;
      // } else {
      this.newForm.quote.forEach((item) => {
        newTotal += item.nums * Number(item.domesticGoodsPrice);
      });
      this.newForm.totalPrice = newTotal;
      // }
      this.total = Number(this.form.totalPrice) + this.newForm.totalPrice;
    },
    // 基础配置删除
    handleDelete(index) {
      this.form.quote.splice(index, 1);
      // let total = 0;
      // this.form.quote.forEach((item) => {
      //   total += item.nums * Number(item.domesticGoodsPrice);
      // });
      // this.form.totalPrice = total;
      // this.total = this.form.totalPrice + this.newForm.totalPrice;
    },
    // 可选配置删除‘
    newHandleDelete(index) {
      this.newForm.quote.splice(index, 1);
      let newTotal = 0;
      this.newForm.quote.forEach((item) => {
        newTotal += item.nums * Number(item.domesticGoodsPrice);
      });
      this.newForm.totalPrice = newTotal;
      this.total = this.form.totalPrice + this.newForm.totalPrice;
    },
    // 上传模板
    async upForm() {
      this.loading = true
      let groupMethod = this.form.groupMethod,
        nodeId = this.form.nodeId,
        quoteGroupId1 = this.form.quoteGroupId,
        quoteGroupId2 = this.newForm.quoteGroupId
      const formData = new FormData();
      formData.append('file', this.file);
      formData.append('groupMethod', groupMethod);
      formData.append('nodeId', nodeId);
      if (this.title != '新增') {
        formData.append('quoteGroupId1', quoteGroupId1);
        formData.append('quoteGroupId2', quoteGroupId2);
      } else {
        formData.append('quoteGroupId1', 0);
        formData.append('quoteGroupId2', 0);
      }
      try {
        const response = await axios.post(process.env.VUE_APP_BASE_API + '/maintain/maintain/quote/importQuoteData', formData, {
          headers: {
            'Authorization': Cookie.get('Admin-Token')
          }
        });
        if (response.data.code == 200) {
          console.log('response', response);
          this.$message.success('成功导入数据~');
          // this.$emit('changeopen')
          this.importOpen = false
          // this.file = null
        } else {
          this.$message.error('导入失败，请检查模板~');
        }
        this.getInfo = {
          groupMethod: this.form.groupMethod,
          nodeId: this.form.nodeId,
        }
        details(this.getInfo).then(res => {
          const data = res.data;
          this.$nextTick(() => {
            data.forEach(item => {
              if (item.configurationType == 1) {
                this.form = item;
              } else if (item.configurationType == 2) {
                this.newForm = item;
              }
              this.total += item.totalPrice;
            })
            // this.$refs.addInfo.form = res.data;
            this.loading = false
          })
        });
      } catch (error) {
        console.log('error', error);
        this.loading = false
      }
      return
      // importQuoteData().then(res=>{

      // }).catch()
    },
    // 下载模板
    modeDown() {
      let url = process.env.BASE_URL + 'static/报价标准模板.xlsx'
      const link = document.createElement('a');
      link.href = url;
      link.setAttribute('download', ''); // 设置文件属性为下载
      document.body.appendChild(link); // 将元素插入到 DOM 中
      link.click(); // 模拟点击下载链接进行下载
      document.body.removeChild(link); // 下载完成后移除元素
    },
    // 
    handleFileUpload({ target }) {
      this.file = target.files[0];
      console.log('this.file', this.file);
    },
    // 上传标准表单
    openForm() {
      this.$refs.formone.validate((valid) => {
        console.log(valid);
        if (valid) {
          this.importOpen = true
        }
      });
    }
  },
};
</script>

<style scopoed lang="scss">
.add-table {
  width: 98%;
  max-height: 500px;
  border: 1px solid #e7e7e7;
  position: relative;
  overflow-y: auto;
  padding-bottom: 20px;
  box-sizing: border-box;
  .add-table-title {
    width: 100%;
    background-color: #f6f6f6;
    div {
      line-height: 42px;
      border-right: 1px solid #e7e7e7;
      border-bottom: 1px solid #e7e7e7;
      text-align: left;
      color: #999;
      &:last-child {
        border-right: none;
      }
    }
  }
  .add-table-list {
    margin-top: 15px;
    div {
      padding-right: 5px;
      div {
        padding: 0;
      }
    }
    .el-input-number--medium {
      width: 100%;
    }
  }
}
.add-btn {
  position: absolute;
  right: 20px;
  top: 180px;
}
.addd-table-column {
  display: flex;
  div {
    font-size: 16px;
    padding-left: 10px;
  }
}
.ordinary-btn {
  width: 80px;
  height: 32px;
  border: 1px solid #dcdcdc;
  font-size: 16px;
  span {
    color: #333333e6;
  }
}
.newAddBtn {
  position: relative;
  .add-btn {
    right: 0;
    top: 110px;
  }
}
.addBox {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 10px;
}

.fileParBox {
  position: relative;
  width: 100px;
  height: 100px;
  line-height: 100px;
  background-color: #dedede;
  text-align: center;
  border-radius: 10px;
}

#file-upload {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  opacity: 0;
}
</style>